package net.xumf.web.controller.quartz;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xumf.service.master.QuartzService;
import net.xumf.support.consts.Constants;
import net.xumf.support.model.ResponseResult;
import net.xumf.support.model.vo.CronTriggerVO;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "定时管理", description = "定时")
@RestController
@RequestMapping(value = "quartz")
public class QuartzController {

    @Autowired
    private QuartzService quartzService;


    @ApiOperation(value = "获取所有定时任务数据")
    @GetMapping(value = Constants.UrlPath.GET_ALL)
    public List<CronTriggerVO> getAllJob() throws SchedulerException {
        return quartzService.getAllJob();
    }

    @ApiOperation(value = "暂停定时任务")
    @ApiParam
    @PostMapping(value = "/pause")
    public ResponseResult pauseJob(@ApiParam(name = "任务名", value = "jobName", required = true) @RequestParam(value = "jobName") String jobName) throws SchedulerException {
        quartzService.jobPause(jobName);
        return ResponseResult.success();
    }

    @ApiOperation(value = "重启定时任务")
    @PostMapping(value = "/resume")
    public ResponseResult resumeJob(@ApiParam(name = "任务名", value = "jobName", required = true) @RequestParam(value = "jobName") String jobName) throws SchedulerException {
        quartzService.jobResume(jobName);
        return ResponseResult.success();
    }

    @ApiOperation(value = "删除定时任务")
    @PostMapping(value = Constants.UrlPath.ID_PATH_ACTION)
    public ResponseResult deleteJob(@ApiParam(name = "任务名", value = "jobName", required = true) @RequestParam(value = "jobName") String jobName) throws SchedulerException {
        quartzService.jobDelete(jobName);
        return ResponseResult.success();
    }
}
